﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Janus.Cores.Configuration
{
    public class MemCacheConfigSection : ConfigurationSection
    {
        [ConfigurationProperty("memConfig")]
        public MemCacheElement MemConfig
        {
            get { return this["memConfig"] as MemCacheElement; }
            set { this["memConfig"] = value; }
        }

        [ConfigurationProperty("servers")]
        public ServersElementCollection Servers
        {
            get { return this["servers"] as ServersElementCollection; }
            set { this["Servers"] = value; }
        }

    }
    public class MemCacheElement : System.Configuration.ConfigurationElement
    {
        /// <summary>
        /// 初始连接数 
        /// </summary>
        [ConfigurationProperty("initConnections")]
        public int InitConnections
        {
            get { return (int)this["initConnections"]; }
            set { this["initConnections"] = value; }
        }

        /// <summary>
        /// 最小连接数 
        /// </summary>
        [ConfigurationProperty("minConnections")]
        public int MinConnections
        {
            get { return (int)this["minConnections"]; }
            set { this["minConnections"] = value; }
        }

        /// <summary>
        /// 最小连接数 
        /// </summary>
        [ConfigurationProperty("maxConnections")]
        public int MaxConnections
        {
            get { return (int)this["maxConnections"]; }
            set { this["maxConnections"] = value; }
        }

        /// <summary>
        /// 设置连接的套接字超时  
        /// </summary>
        [ConfigurationProperty("socketConnectTimeout")]
        public int SocketConnectTimeout
        {
            get { return (int)this["socketConnectTimeout"]; }
            set { this["socketConnectTimeout"] = value; }
        }

        /// <summary>
        /// 设置套接字超时读取  
        /// </summary>
        [ConfigurationProperty("socketTimeout")]
        public int SocketTimeout
        {
            get { return (int)this["socketTimeout"]; }
            set { this["socketTimeout"] = value; }
        }

        /// <summary>
        /// 设置维护线程运行的睡眠时间。如果设置为0，那么维护线程将不会启动,30就是每隔30秒醒来一次    
        /// </summary>
        [ConfigurationProperty("maintenanceSleep")]
        public int MaintenanceSleep
        {
            get { return (int)this["maintenanceSleep"]; }
            set { this["maintenanceSleep"] = value; }
        }

        /// <summary>
        /// 获取或设置池的故障标志,false，则得到一个套接字如果存在的话。否则返回NULL，如果它无法连接到请求的服务器;
        /// true则socket连接失败，将试图从另一台服务器返回一个套接字如果存在的话      
        /// </summary>
        [ConfigurationProperty("failover")]
        public bool Failover
        {
            get { return (bool)this["failover"]; }
            set { this["failover"] = value; }
        }

        /// <summary>
        /// 对所有创建的套接字是否关闭Nagle的算法
        /// </summary>
        [ConfigurationProperty("nagle")]
        public bool Nagle
        {
            get { return (bool)this["nagle"]; }
            set { this["nagle"] = value; }
        }

    }

    public class ServerElement : System.Configuration.ConfigurationElement
    {
        /// <summary>
        /// 服务器地址 
        /// </summary>
        [ConfigurationProperty("address")]
        public string Address
        {
            get { return this["address"] as string; }
            set { this["address"] = value; }
        }

        /// <summary>
        /// 端口 
        /// </summary>
        [ConfigurationProperty("port")]
        public int Port
        {
            get { return (int)this["port"]; }
            set { this["port"] = value; }
        }

        /// <summary>
        /// 权重 
        /// </summary>
        [ConfigurationProperty("weight")]
        public int Weight
        {
            get { return (int)this["weight"]; }
            set { this["weight"] = value; }
        }
        
        public override string ToString()
        {
            return string.Format("{0}:{1}", Address, Port);
        }
    }
    public class ServersElementCollection : ConfigurationElementCollection
    {
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        protected override ConfigurationElement CreateNewElement()
        {
            return new ServerElement();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="element"></param>
        /// <returns></returns>
        protected override object GetElementKey(ConfigurationElement element)
        {
            var el = (element as ServerElement);
            return string.Format("{0}:{1}", el.Address, el.Port);
        }

        public override ConfigurationElementCollectionType CollectionType
        {
            get { return ConfigurationElementCollectionType.BasicMap; }
        }
        protected override string ElementName
        {
            get { return "add"; }
        }
    }

}
